package com.ultrawise.survey.module.surevybase.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ultrawise.survey.common.printfile.FreeMarkertUtil;
import com.ultrawise.survey.common.printfile.TypeToHtml;
import com.ultrawise.survey.common.utils.DateUtils;
import com.ultrawise.survey.contants.SurveyConstants;
import com.ultrawise.survey.core.Page;
import com.ultrawise.survey.core.base.BaseService;
import com.ultrawise.survey.core.base.ServiceException;
import com.ultrawise.survey.module.surevybase.dao.TSurveyInfoBaseDAO;
import com.ultrawise.survey.module.surevybase.dao.TSurveyInfoObjtableDAO;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoBase;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoObjtable;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoQuestion;
import com.ultrawise.survey.module.surevybase.vo.TSurveyLogPublish;

@Service("tSurveyInfoBaseService")
public class TSurveyInfoBaseService extends BaseService {

	@Autowired
	TSurveyInfoBaseDAO tSurveyInfoBaseDAO;

	@Autowired
	TSurveyInfoObjtableDAO tSurveyInfoObjtableDAO;

	/***
	 * 返回某个问卷的全部信息
	 * 
	 * @param syid
	 * @return
	 */
	public Map<String, Object> getSurveyAll(Integer syid) {
		TSurveyInfoBase bInfoBase = (TSurveyInfoBase) tSurveyInfoBaseDAO
				.findByID(syid, new TSurveyInfoBase(), TSurveyInfoBase.class);
		List<TSurveyInfoQuestion> lsquestList = (List<TSurveyInfoQuestion>) tSurveyInfoBaseDAO
				.findByColunm("SY_ID", syid.toString(),
						new TSurveyInfoQuestion(), TSurveyInfoQuestion.class);
		String html = "";
		for (int i = 0; lsquestList != null && i < lsquestList.size(); i++) {
			TSurveyInfoQuestion tq = lsquestList.get(i);
			tq.setQn_name(tq.getQn_name() + "<br/>");
			tq.setQn_options(TypeToHtml.getHtmlByType(tq.getQn_type(),
					tq.getQn_options(), tq.getId()));
		}

		List<TSurveyInfoObjtable> tobj = tSurveyInfoObjtableDAO.getBySyidOrder(
				"SY_ID", syid.toString());

		//增加script来验证添加调查对象的验证信息
		String script = "";
		for(TSurveyInfoObjtable eobj:tobj){
			if("2".equalsIgnoreCase(eobj.getOb_type())){
				script+="  isval=isval && (isNum(document.getElementById('"+eobj.getOb_code()+"').value,'"+eobj.getOb_name()+"'));\n\t";
				continue;
			}
//			if("3".equalsIgnoreCase(eobj.getOb_type())){
//				script+=" doucment.getElementById('"+eobj.getOb_code()+"').onfocus=function(){return new WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true});}; \n\t";
//				continue;
//			}
			if("5".equalsIgnoreCase(eobj.getOb_type())){
				script+="  isval=isval && (IdentityCodeValid(document.getElementById('"+eobj.getOb_code()+"').value));\n\t";
				continue;
			}
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("base", bInfoBase);
		map.put("question", lsquestList);
		map.put("tobj", tobj);
		map.put("script", script);
		return map;
	}

	/***
	 * 发布内容，並且返回輸出文件名。
	 * 
	 * @param syid
	 * @return
	 */
	public String ispublish(Integer syid) {
		Map<String, Object> baseinfo = getSurveyAll(syid);
		TSurveyInfoBase bInfoBase = (TSurveyInfoBase) baseinfo.get("base");
		String outfilename = "";
		try {
			outfilename = FreeMarkertUtil.getNewHtml(
					bInfoBase.getSy_template(), baseinfo, bInfoBase.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return outfilename;
	}

	
	public String ispubview(Integer syid) {
		Map<String, Object> baseinfo = getSurveyAll(syid);
		TSurveyInfoBase bInfoBase = (TSurveyInfoBase) baseinfo.get("base");
		String outfilename = "view"+syid;
		try {
			//???
			outfilename = FreeMarkertUtil.getNewViewHtml(
					bInfoBase.getSy_template(), baseinfo, bInfoBase.getId(),outfilename);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return outfilename;
	}
	/****
	 * 返回全部问卷类表信息，根据翻页信息
	 * 
	 * @param start
	 * @param limel
	 * @param baseinfo
	 * @return
	 * @throws ServiceException
	 */
	public Page<TSurveyInfoBase> getlistpage(int start, int limel,
			TSurveyInfoBase baseinfo) throws ServiceException {
		try {
			Page<TSurveyInfoBase> page = null;
			page = tSurveyInfoBaseDAO.getlistpage(start, limel, baseinfo);
			return page;
		} catch (Exception e) {
			throw new ServiceException("查询失败", e);
		}
	}

	
	/*****
	 * 获取当前正在进行的问卷调查信息
	 * @return
	 */
	public List<TSurveyInfoBase> getStarSurveyInfo() {
		return tSurveyInfoBaseDAO.getStarSurveyInfo();
	}
	
}
